package com.jishuo.pzk.api;

import com.hazelcast.core.HazelcastInstance;
import com.jishuo.base.annotation.UserAuthority;
import com.jishuo.base.bean.BaseResult;
import com.jishuo.base.bean.GridResult;
import com.jishuo.base.bean.Pagination;
import com.jishuo.pzk.bean.base.VoucherFileVO;
import com.jishuo.pzk.bean.base.VoucherTypeVO;
import com.jishuo.pzk.bean.base.VoucherVO;
import com.jishuo.pzk.bean.dto.SelectReq;
import com.jishuo.pzk.job.TaskJob;
import com.jishuo.pzk.service.*;
import com.jishuo.pzk.util.VoucherUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 消息接口
 */
@Controller
@RequestMapping(value = "/api")
@Slf4j
@UserAuthority(menu = {"bill/voucher"})
public class MsgWeb {

    @Resource
    private HttpServletRequest request;

    @Resource
    private HazelcastInstance hazelcastInstance;

    @Resource
    private VoucherService voucherService;

    @Resource
    private EticketNumService eticketNumService;

    @Resource
    private StudentService studentService;

    @Resource
    private BackBillService backBillService;

    @Resource
    private FiscService fiscService;

    @Resource
    private BalanceService balanceService;

    @Resource
    private WaitAffirmService waitAffirmService;

    @Resource
    private DivideService divideService;

    @Resource
    private SpecialService specialService;

    @Resource
    private IllegalActivityService illegalActivityService;

    @Resource
    private DeductService deductService;

    @Resource
    private WaitAffirmPayInfoService waitAffirmPayInfoService;

    @Resource
    private IllegalActivityScaleInfoService illegalActivityScaleInfoService;

    @Resource
    private ChargeBillService chargeBillService;

    @Resource
    private PzkService pzkService;

    @Resource
    private VoucherUtil voucherUtil;

    @Resource
    private PzmessageService pzmessageService;

    /**
     * 处理各种类型票据的接收与保存。
     *
     * @param action 表示操作类型的字符串，对应不同票据的保存操作。
     * @param data 表示要保存的票据数据。
     * @return 返回一个表示操作结果的BaseResult对象，成功则返回空结果，失败则返回自定义错误信息。
     */
    @ResponseBody
    @RequestMapping(value = "revMsg")
    public BaseResult revMsg(String action, String data) {
        int ret = 0;
        // 根据不同的action调用相应的服务保存数据
        if ("SETETICKETPZK".equals(action)) {
            ret = eticketNumService.savePzk(data); // 电子票
        } else if ("SETSTUDENTPZK".equals(action)) {
            ret = studentService.savePzk(data); // 学生票
        } else if ("SETBACKBILLPZK".equals(action)) {
            ret = backBillService.savePzk(data); // 退付书
        } else if ("SETBANKPAYFISCPZK".equals(action)) {
            ret = fiscService.savePzk(data); // 国库
        } else if ("SETBANKBALANCEPZK".equals(action)) {
            ret = balanceService.savePzk(data); // 余额
        } else if ("SETWAITAFFIRMPZK".equals(action)) {
            ret = waitAffirmService.savePzk(data); // 待查收入
        } else if ("SETPAYDIVIDEPZK".equals(action)) {
            ret = divideService.savePzk(data); // 分成
        } else if ("SETSPECIALPZK".equals(action)) {
            ret = specialService.savePzk(data); // 专户
        } else if ("SETPAYINFOPZK".equals(action)) {
            ret = illegalActivityService.savePzk(data); // 交罚
        } else if ("SETDEDUCTPZK".equals(action)) {
            ret = deductService.savePzk(data); // 批扣
        } else if ("SETWAITAFFIRMPAYINFOPZK".equals(action)) {
            ret = waitAffirmPayInfoService.savePzk(data); // 待查支出
        } else if ("SETBANKILLEGALACTIVITYSCALEINFOPZK".equals(action)) {
            ret = illegalActivityScaleInfoService.savePzk(data); // 交管二八分成
        } else if ("SETCHARGEBILLPZK".equals(action)) {
            ret = chargeBillService.savePzk(data); // 缴款书
        } else if ("SETVOUCHERELIMINATION".equals(action)) {
            ret = voucherUtil.voucherElimination(data); // 凭证冲销
        }
        // 根据操作结果返回相应的BaseResult对象
        return (ret != 0) ? new BaseResult() : BaseResult.CustomErr("接收失败");
    }

    /**
     * 凭证入库方法
     *
     * @return 无参
     */
    @ResponseBody
    @RequestMapping(value = "imp")
    public BaseResult imp() {
        pzmessageService.impData();
        return new BaseResult();
    }

    /**
     * 导航凭证主页面
     *
     * @return html页面路径
     */
    @GetMapping("/gotoList")
    public String gotoUnitVsItem() {
        return "pzk/voucher";
    }

    /**
     * 立即执行一次定时任务
     *
     * @return 无参
     * @throws Exception 异常抛出
     */
    @ResponseBody
    @RequestMapping(value = "draw")
    public BaseResult draw() throws Exception {
        pzkService.draw();
        return new BaseResult();
    }

    /**
     * 查询凭证列表
     *
     * @param req 请求
     * @return 凭证列表
     */

    @ResponseBody
    @RequestMapping(value = "selectVoucher", name = "凭证列表")
    public GridResult<VoucherVO> selectVoucher(SelectReq req, Pagination pagination) {
        log.info("接口功能：【{}】，请求地址：【{}】，请求方式：【{}】，请求参数：【{}】",
                "查询凭证列表", request.getRequestURI(), request.getMethod(), req);

        GridResult<VoucherVO> result = new GridResult<>(voucherService.selectVoucher(req, pagination));

        log.info("接口功能：【{}】，请求地址：【{}】，请求方式：【{}】，响应参数：【↑↑↑↑↑查看上方SQL日志↑↑↑↑↑】",
                "查询凭证列表", request.getRequestURI(), request.getMethod());

        return result;
    }

    /**
     * 查询所有的凭证类型
     *
     * @return 凭证类型列表
     */
    @ResponseBody
    @PostMapping(value = "selectVoucherType", name = "查询凭证类型列表")
    public GridResult<VoucherTypeVO> selectVoucherType() {
        log.info("接口功能：【{}】，请求地址：【{}】，请求方式：【{}】，请求参数：【无参】",
                "查询凭证类型列表", request.getRequestURI(), request.getMethod());

        List<VoucherTypeVO> result = voucherService.selectVoucherType();
        // GridResult<List<VoucherTypeVO>> result = new GridResult<>( voucherService.selectVoucherType(););
        GridResult<VoucherTypeVO> voucherTypeVOGridResult = new GridResult<>(result);
        log.info("接口功能：【{}】，请求地址：【{}】，请求方式：【{}】，响应参数：【↑↑↑↑↑查看上方SQL日志↑↑↑↑↑】",
                "查询凭证类型列表", request.getRequestURI(), request.getMethod());

        return voucherTypeVOGridResult;
    }

    /**
     * 查看已画图的凭证图片列表
     *
     * @param req 包括票号和sn码
     * @return 凭证图片列表
     */
    @ResponseBody
    @PostMapping(value = "lookVoucher", name = "根据条件查询已画图的凭证")
    public GridResult<VoucherFileVO> lookVoucher(SelectReq req) {
        log.info("接口功能：【{}】，请求地址：【{}】，请求方式：【{}】，请求参数：【{}】",
                "查看已画图的凭证图片列表", request.getRequestURI(), request.getMethod(), req);

        GridResult<VoucherFileVO> result = new GridResult<>(voucherService.findVoucherFile(req));

        log.info("接口功能：【{}】，请求地址：【{}】，请求方式：【{}】，响应参数：【↑↑↑↑↑查看上方SQL日志↑↑↑↑↑】",
                "查看已画图的凭证图片列表", request.getRequestURI(), request.getMethod());

        return result;
    }

    /**
     * 从缓存中获取数据。
     * <p>该方法不需要参数，通过GET请求访问路径"/qu"来触发。</p>
     * <p>不返回任何数据，而是将获取到的数据通过日志输出。</p>
     */
    @GetMapping("/qu")
    public void getDataFromCache() {
        // 从名为"yaoweiqi"的Map缓存中获取键为"one"的值
        String value = (String) hazelcastInstance.getMap("yaoweiqi").get("one");
        log.info("您取到的数据是：" + value);
    }

    /**
     * 将数据存入缓存。
     * <p>该方法不需要参数，通过GET请求访问路径"/cun"来触发。</p>
     * <p>不返回任何数据，而是将指定数据存入缓存，并通过日志确认存入操作。</p>
     */
    @GetMapping("/cun")
    public void putDataInCache() {
        // 将键值对"two"："恭喜你又取到了这组数据！！！！！！"存入名为"yaoweiqi"的Map缓存中
        hazelcastInstance.getMap("yaoweiqi").put("two", "恭喜你又取到了这组数据！！！！！！");
        log.info("存入成功！");
    }
}
